<?php
namespace app\common\controller;

use Org\MyCustomCache;
use think\cache\driver\Redis;
use think\Controller;
use EasyWeChat\Factory;

class Mobile extends Controller
{
    public function initialize()
    {
        parent::initialize();
        $this->jssdk();
    }

    /**
     * jssdk config
     * @return [type] [description]
     */
    public function jssdk()
    {
        if (request()->isAjax()) {
            return true;
        }
        $config = config('account.easywechat');
        $app = Factory::officialAccount($config);
        // $app->jssdk->setUrl($url); // 会自动获取当前url，一般不需要重新设置
        $jssdk=json_encode([]);

     //        $redis = new Redis();
     //        $redis->connect('redis_host', 6379);
        $cache=new MyCustomCache();
        $app->rebind('cache',$cache);
        try {

            $jssdk = $app->jssdk->buildConfig(['onMenuShareTimeline', 'onMenuShareAppMessage', 'updateAppMessageShareData', 'updateTimelineShareData', 'chooseWXPay'], true);

        } catch (\Exception $e) {

        }

        $this->assign('jssdk', $jssdk);
    }


    public function setRedirct($redirect_url='')
    {
        if (!session('?redirect_url')) {
            session('redirect_url', $redirect_url ?: request()->param('redirect_url'));
        }
    }
}
